package com.hj.service.impl;

import com.hj.dao.CompanyMapper;
import com.hj.dao.CustomerResetMapper;
import com.hj.dao.ReSetMapper;
import com.hj.entity.*;
import com.hj.utils.MailHelper;
import com.hj.utils.MultipleDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

@Component
public class SpecialResetMission {

    @Autowired
    private ReSetMapper resetmapper;

    @Autowired
    private CompanyMapper companymapper;

    @Autowired
    private CustomerResetMapper customerReSetMapper;

    @Autowired
    private MailHelper mailHelper;

    @Value("${cp.agency.need.closedown}")
    private String agencyNames;

    @Scheduled(cron="0 0/4 * * * ?")
    public void specialAgencyResetJob() {

        String[] agencys = agencyNames.split(",");

        /*查询停机状态的400号码*/
        MultipleDataSource.setDataSourceKey("dataSource");
        List<Ss_accesscode> list_sac=resetmapper.search_ac();

        MultipleDataSource.setDataSourceKey("dataSource2");
        //查询代理商商户余额大于0且截止日期大于当前时间的号码
        List<Company> cp=customerReSetMapper.search_cp(agencys,list_sac);

        if(cp.size()>0){

            //先修改company表状态
            companymapper.update_companyReset(cp);

            //查询上一次停机前的接入策略并复机
            Iterator<Company> companyIterator = cp.iterator();
            while(companyIterator.hasNext()){
                List<Ss_accesscode> accesscodeList = resetmapper.search_former(companyIterator.next());
                if(accesscodeList.size()>0){
                    for(Ss_accesscode ss_accesscode : accesscodeList){
                        MultipleDataSource.setDataSourceKey("dataSource");
                        resetmapper.update_former(ss_accesscode);

                        MultipleDataSource.setDataSourceKey("dataSource2");
                        resetmapper.delete_formerRecord(ss_accesscode);
                    }
                    companyIterator.remove();
                }
            }

            //查询绑定的是中继号并邮件通知复机
            List<MiddleNumber> middleNumberList = companymapper.search_middle_number(cp);
            if(middleNumberList.size()>0){
                String middleNumberUser = "";
                for(MiddleNumber middleNumber:middleNumberList){
                    Iterator<Company> iterator = cp.iterator();
                    //去除绑定中继号的400号码
                    if(iterator.hasNext()){
                        if(iterator.next().getCallee_number().equals(middleNumber.getCallee_number())){
                            iterator.remove();
                        }
                    }
                    middleNumberUser += middleNumber.getCallee_number()+" ";
                }
                //邮件通知维护人员进行手动复机
                mailHelper.mailto("", "停复机服务——中继号复机", middleNumberUser);
            }

            MultipleDataSource.setDataSourceKey("dataSource");

            if(cp.size()>0){
                //查询需按路由设置的400号码并复机
                List<Cti_routeservice> c_rs=resetmapper.search_crs(cp);
                if(c_rs.size()>0){
                    for(Cti_routeservice cti_routeservice:c_rs){
                        Iterator<Company> iterator = cp.iterator();
                        //去除绑定路由的400号码
                        if(iterator.hasNext()){
                            if(iterator.next().getVccid().equals(cti_routeservice.getVccid())){
                                iterator.remove();
                            }
                        }
                        resetmapper.update_rs(cti_routeservice);
                    }
                }
            }

            if(cp.size()>0) {
                //查询需按ivr设置的400号码并复机
                List<Cti_ivrservice> c_is = resetmapper.search_is(cp);
                if (c_is.size() > 0) {
                    for (Cti_ivrservice cti_ivrservice : c_is) {
                        //去除绑定ivr的400号码
                        Iterator<Company> iterator = cp.iterator();
                        //去除绑定路由的400号码
                        if (iterator.hasNext()) {
                            if (iterator.next().getVccid().equals(cti_ivrservice.getVccId())) {
                                iterator.remove();
                            }
                        }
                        resetmapper.update_is(cti_ivrservice);
                    }
                }
            }

            if(cp.size()>0) {
                //查询需按人工设置的400号码并复机
                List<Cti_humanservice> c_hs = resetmapper.search_hs(cp);
                if (c_hs.size() > 0) {
                    for (Cti_humanservice cti_humanservice : c_hs) {
                        resetmapper.update_hs(cti_humanservice);
                    }
                }
            }
        }
    }
}
